package com.tc.controller;

import com.tc.base.shiro.UserContext;
import com.tc.service.LyjqService;
import com.tc.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lzy83 on 16/4/5.
 */
@Controller
@RequestMapping("/lyjq")
public class LyjqController {

    @Autowired
    LyjqService lyjqService;

    @RequestMapping(value = "/toLyjq")
    public String toLyjq(){
        return "lyjq/LyjqTj";
    }

    @RequestMapping(value = "listTjx", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> listTjx(HttpServletRequest request) {
        UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,String> listMap=new HashMap<String,String>();
        listMap.put("type",request.getParameter("type"));
        listMap.put("creater",user.getUserId());
        List<Map<String,Object>> list=lyjqService.selectTjx(listMap);
        map.put("msg",list);
        return map;
    }



    @RequestMapping(value = "saveTjx", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> saveTjx(HttpServletRequest request) {

        UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,String> saveMap=new HashMap<String,String>();
        saveMap.put("name",request.getParameter("name"));
        saveMap.put("type",request.getParameter("type"));
        List<Map<String,Object>> list=lyjqService.selectTjx(saveMap);
        if(list.size()>0){
            map.put("msg", "已存在此记录");
        }else{
            saveMap.put("creater",user.getUserId());
            lyjqService.addTjx(saveMap);
            map.put("msg", "success");

        }
        return map;
    }

    @RequestMapping(value = "saveTjjg", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> saveTjjg(HttpServletRequest request) {

        UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,String> saveMap=new HashMap<String,String>();
        saveMap.put("tjxid",request.getParameter("tjxid"));
        saveMap.put("tjtime",request.getParameter("tjtime"));
        List<Map<String,Object>> list=lyjqService.selectTjjg(saveMap);
        if(list.size()>0){
            map.put("msg", "已存在此记录");
        }else{
            saveMap.put("tjjg",request.getParameter("tjjg"));
            lyjqService.addTjjg(saveMap);
            map.put("msg", "success");
        }
        return map;
    }


    @RequestMapping(value = "startSearch", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> startSearch(HttpServletRequest request) {
        UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,String> listMap=new HashMap<String,String>();
        listMap.put("type",request.getParameter("type"));
        List<Map<String,Object>> allTjxUnderType=lyjqService.findTjxByType(listMap);
        listMap.put("starttime",request.getParameter("starttime"));
        listMap.put("endtime",request.getParameter("endtime"));
        listMap.put("creater",user.getUserId());
        for (Map<String ,Object> tjx:allTjxUnderType) {
            listMap.put("tjxid",tjx.get("id").toString());
            List<Map<String,Object>> list=lyjqService.tjLyjq(listMap);
            map.put(tjx.get("name").toString(),list);
        }
        return map;
    }




}
